package cn.aib.eduservice.controller;


import cn.aib.commonutils.R;
import cn.aib.eduservice.entity.EduCourse;
import cn.aib.eduservice.entity.vo.CourseInfoVo;
import cn.aib.eduservice.entity.vo.CoursePublishVo;
import cn.aib.eduservice.entity.vo.CourseQuery;
import cn.aib.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-12-21
 */
@RestController
@RequestMapping("/eduservice/course")
//@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;

    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody(required = false) CourseInfoVo courseInfo){
        //调用service完成课程添加
        String courseId = eduCourseService.addCourse(courseInfo);

        return R.ok().data("courseId",courseId);
    }

    //根据课程ID回显数据
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        //调用service完成课程添加
        //由于表单的信息包括课程和课程描述，因此返回的对象中应该包含这两部分信息
        CourseInfoVo courseInfoVo = eduCourseService.getCourseInfoById(courseId);

        return R.ok().data("courseInfoVo",courseInfoVo);
    }

    //修改课程数据
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        //调用service完成课程修改
        //修改课程数据
        eduCourseService.updateCourseInfo(courseInfoVo);

        return R.ok();
    }

    //根据课程ID回显最终发布的课程信息
    @GetMapping("getPublishCourseInfo/{courseId}")
    public R updateCourseInfo(@PathVariable String courseId){
        //调用service完成课程修改
        //修改课程数据
        CoursePublishVo publishCourse = eduCourseService.getPublishCourse(courseId);

        return R.ok().data("publishCourse",publishCourse);
    }

    //修改课程状态
    @PostMapping("publishCourse/{courseId}")
    public R publishCourse(@PathVariable String courseId){
        EduCourse eduCourse = new EduCourse();

        eduCourse.setId(courseId);
        eduCourse.setStatus("Normal");

        //修改课程状态
        boolean flag = eduCourseService.updateById(eduCourse);
        if(flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    /**
     * 课程多条件组合分页查询
     * @param current：当前页
     * @param limit：每页记录数
     * @param courseQuery：封装的条件对象
     * @return
     */
    @PostMapping("/pageCourseCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current, @PathVariable long limit,
                                  @RequestBody(required = false) CourseQuery courseQuery){
        //创建page对象，该对象封装了所有的分页数据
        Page<EduCourse> pageCourse = new Page<>(current,limit);

        QueryWrapper<EduCourse> queryWrapper = null;

        //判断条件是否为空
        if(courseQuery != null && !StringUtils.isEmpty(courseQuery)){
            //创建wrapper对象，判断属性是否为空添加条件
            queryWrapper = new QueryWrapper<>();

            //取teacherQuery的属性值
            String title = courseQuery.getTitle();
            String status = courseQuery.getStatus();

            //判断进行拼接条件
            if(!StringUtils.isEmpty(title)){
                queryWrapper.like("title", title);
            }

            if(!StringUtils.isEmpty(status)){
                queryWrapper.eq("status", status);
            }

            //根据创建时间降序排序
            queryWrapper.orderByDesc("gmt_create");
        }

        //查询数据库数据和分页信息并封装到page对象中;第二个参数为null，代表条件为空
        eduCourseService.page(pageCourse, queryWrapper);

        //获取总记录数和分页数据
        long total = pageCourse.getTotal();
        List<EduCourse> records = pageCourse.getRecords();

        //创建一个map
        Map<String, Object> map = new HashedMap<>();
        //放入数据到map
        map.put("total", total);
        map.put("records", records);

        //返回
        return R.ok().data(map);
    }


    //删除课程状态
    @DeleteMapping("deleteCourse/{courseId}")
    public R deleteCourse(@PathVariable String courseId){

        R flag = null;

        try {
            //调用eduCourseService删除课程
            flag = eduCourseService.deleteCourse(courseId);

            return flag;
        }catch (Exception e ){
            e.printStackTrace();
            return R.error();
        }
    }
}

